/*
 * @lc app=leetcode.cn id=189 lang=swift
 *
 * [189] 旋转数组
 */

// @lc code=start
class Solution189 {
    func rotate(_ nums: inout [Int], _ k: Int) {
        let kk = k % nums.count
        rotateLocal(&nums, start: 0, end: nums.count - kk - 1)
        rotateLocal(&nums, start: nums.count - kk, end: nums.count - 1)
        rotateLocal(&nums, start: 0, end: nums.count - 1)
    }
    func rotateLocal(_ nums: inout [Int], start: Int, end: Int) {
        var left = start
        var right = end
        while left < right {
            let tmp = nums[left]
            nums[left] = nums[right]
            nums[right] = tmp
            left += 1
            right -= 1
        }
    }
    class func test()  {
        var array  = [1,2,3,4,5,6,7]
        Solution189().rotate(&array, 3)
        print(array)
    }
}
// @lc code=end

